<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-header">
        <img src="/vite.svg" alt="Logo" class="login-logo" />
        <h1 class="login-brand">后台管理系统</h1>
      </div>
      
      <a-card class="login-card">
        <template #title>
          <div>
            <h2 class="login-title">欢迎登录</h2>
            <p class="login-subtitle">请使用您的账号密码登录系统</p>
          </div>
        </template>
        
        <a-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          layout="vertical"
          @keypress.enter="handleLogin"
        >
          <a-form-item name="account">
            <a-input
              v-model:value="loginForm.account"
              placeholder="请输入用户名"
              size="middle"
              class="login-input"
            >
              <template #prefix>
                <user-outlined />
              </template>
            </a-input>
          </a-form-item>
          
          <a-form-item name="password">
            <a-input-password
              v-model:value="loginForm.password"
              placeholder="请输入密码"
              size="middle"
              class="login-input"
            >
              <template #prefix>
                <lock-outlined />
              </template>
            </a-input-password>
          </a-form-item>
          
          <a-form-item class="login-options">
            <a-checkbox v-model:checked="rememberMe">记住我</a-checkbox>
            <a class="forget-password" type="primary">忘记密码？</a>
          </a-form-item>
          
          <a-form-item>
            <a-button
              type="primary"
              class="login-button"
              :loading="loading"
              size="large"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </a-button>
          </a-form-item>
        </a-form>
      </a-card>
      
      <div class="login-footer">
        <p>Copyright © 2023 Vue3 Admin All Rights Reserved</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../../stores/user'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { login } from '../../api/user'

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref(null)
const loading = ref(false)
const rememberMe = ref(false)

const loginForm = reactive({
  account: 'zhangxu',
  password: '123456'
})

const loginRules = {
  account: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    try {
      // 调用登录接口
      const res = await login(loginForm)
      userStore.setToken(res.data.token)
      
      // 获取用户信息和权限
      await userStore.getUserPermissions()
      
      message.success('登录成功')
      router.push('/')
    } catch (error) {
      console.error('登录错误:', error)
      message.error(error.message || '登录失败')
    } finally {
      loading.value = false
    }
  } catch (error) {
    console.error('表单验证错误:', error)
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-content {
  width: 100%;
  max-width: 440px;
  text-align: center;
}

.login-header {
  margin-bottom: 30px;
}

.login-logo {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
  animation: logo-spin 20s linear infinite;
}

@keyframes logo-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.login-brand {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  color: #ffffff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.login-card {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: none;
  border-radius: 16px;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
}

.login-title {
  margin: 0 0 8px;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
}

.login-subtitle {
  margin: 0;
  font-size: 14px;
  color: #909399;
}

.login-input :deep(.ant-input), .login-input :deep(.ant-input-password) {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  border-radius: 8px;
  height: 44px;
}

.login-input :deep(.ant-input:focus), .login-input :deep(.ant-input-focused) {
  box-shadow: 0 2px 12px 0 rgba(103, 126, 234, 0.2);
}

.login-options {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.forget-password {
  /* margin-left: auto; */
  color: #1890ff;
  cursor: pointer;
  font-size: 14px;
  float: right;
  &:hover {
    text-decoration: underline;
  }
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.login-button:active {
  transform: translateY(0);
}

.login-footer {
  margin-top: 24px;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
}
</style>